﻿/*
 * By David Barrett, Microsoft Ltd. 2012. Use at your own risk.  No warranties are given.
 * 
 * DISCLAIMER:
 * THIS CODE IS SAMPLE CODE. THESE SAMPLES ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND.
 * MICROSOFT FURTHER DISCLAIMS ALL IMPLIED WARRANTIES INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR OF FITNESS FOR
 * A PARTICULAR PURPOSE. THE ENTIRE RISK ARISING OUT OF THE USE OR PERFORMANCE OF THE SAMPLES REMAINS WITH YOU. IN NO EVENT SHALL
 * MICROSOFT OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS,
 * BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING OUT OF THE USE OF OR INABILITY TO USE THE
 * SAMPLES, EVEN IF MICROSOFT HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. BECAUSE SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION
 * OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES, THE ABOVE LIMITATION MAY NOT APPLY TO YOU.
 * */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SOAP_Test_Application
{
    public partial class FormLogViewer : Form
    {
        private ClassLogger _logger=null;

        public FormLogViewer(ClassLogger Logger)
        {
            InitializeComponent();
            _logger = Logger;
            _logger.LogAdded += _logger_OnLogAdded;
            ShowLogIndex();
        }

        ~FormLogViewer()
        {
            try
            {
                _logger.LogAdded -= _logger_OnLogAdded;
            }
            catch { }
        }
        private void ShowLogIndex()
        {
            // Read the logs and update the UI
            listViewLogIndex.Items.Clear();
            richTextBoxLogView.Text = "Please select a log to view.";

            // Check if we have anything to show
            if (_logger.LogHistory == null)
            {
                richTextBoxLogView.Text = "No data found for this session.";
                return;
            }
            if (_logger.LogHistory.Count < 1)
            {
                richTextBoxLogView.Text = "No data found for this session.";
                return;
            }

            // Now read the dates and fill the listbox
            for (int i = 0; i < _logger.LogHistory.Count; i++)
            {
                ListViewItem oItem = new ListViewItem(_logger.LogHistory.Keys.ElementAt(i).ToString("dd MMM yyyy HH:mm:ss"));
                oItem.Tag = _logger.LogHistory.Keys.ElementAt(i);
                string sDetails = _logger.LogHistory[_logger.LogHistory.Keys.ElementAt(i)];
                sDetails = sDetails.Substring(0, sDetails.IndexOf(Environment.NewLine));
                oItem.SubItems.Add(sDetails);
                listViewLogIndex.Items.Add(oItem);
            }
        }

        private void buttonClose_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void _logger_OnLogAdded(object sender, LoggerEventArgs e)
        {
            // New log added
            ListViewItem oItem = new ListViewItem(e.LogTime.ToString("dd MMM yyyy HH:mm:ss"));
            oItem.Tag = e.LogTime;
            string sDetails = e.LogDetails;
            sDetails = sDetails.Substring(0, sDetails.IndexOf(Environment.NewLine));
            oItem.SubItems.Add(sDetails);
            listViewLogIndex.Items.Add(oItem);
        }

        private void listViewLogIndex_SelectedIndexChanged(object sender, EventArgs e)
        {
            richTextBoxLogView.Text = "";
            try
            {
                DateTime oKey = (DateTime)listViewLogIndex.SelectedItems[0].Tag;
                if (_logger.LogHistory.ContainsKey(oKey))
                {
                    string sDetails = _logger.LogHistory[oKey].ToString();
                    sDetails = sDetails.Substring(sDetails.IndexOf(Environment.NewLine)).Trim();
                    richTextBoxLogView.Text = sDetails;
                }
            }
            catch { }
        }

        private void buttonClearLog_Click(object sender, EventArgs e)
        {
            _logger.ClearHistory();
            ShowLogIndex();
        }
    }
}
